home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Codecs / DrawTextCodec / DrawTextCompress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-09  |  4.0 KB  |  212 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3.     Written by:    Mark Krueger
  4.  
  5.     Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  6.  
  7. */
  8.  
  9. #include    <Types.h>
  10. #include    <Memory.h>
  11. #include    "ImageCompression.h"
  12. #include    <QuickDraw.h>
  13.  
  14.  
  15.  
  16. #include    "DrawTextCodec.h"
  17.  
  18. short
  19. CalcError(register char *src,short rowBytes,short index,register char *table);
  20.  
  21.  
  22. /***********************
  23.     
  24.     
  25.     Compress bheight strips ( of bwidth blocks ) of pixels using the given table
  26.     of char values.
  27.     
  28.     Source must be 1-bit/pixel.
  29.     
  30.     Should be called in 32-bit mmu mode, since we access pixels directly.
  31.     
  32.  
  33.  
  34. ***********************/
  35.  
  36. long
  37. Compress(char *baseAddr,short rowBytes,char *pBaseAddr,short pRowBytes,
  38.     short bwidth,short bheight,long spatialQ,long temporalQ,char *dataPtr,
  39.     Fixed *similarityP,SharedGlobals *sGlob)
  40. {
  41.     char *dataStart = dataPtr;
  42.     char *bp;
  43.     register short            i;
  44.     short            x,y;
  45.     short            min = 1000,minIndex,e;
  46.     char            *charTab = sGlob->table;
  47.     short            startIndex = ' ',endIndex = 'z';
  48.     
  49.     
  50.     /* 
  51.     
  52.         limit search based on requested quality level.
  53.         
  54.     */
  55.     
  56.     if ( spatialQ <= codecLowQuality ) {
  57.         startIndex = ' ';
  58.         endIndex = '9';
  59.     }
  60.     else if ( spatialQ <= codecNormalQuality ) {
  61.         startIndex = ' ';
  62.         endIndex = 'z';
  63.     } else  {
  64.         startIndex = 0;
  65.         endIndex = 255;
  66.     }
  67.     
  68.     /* 
  69.     
  70.         compress the strips
  71.         
  72.     */
  73.     
  74.     for ( y=0; y <  bheight; y++ ) {
  75.         bp = baseAddr;
  76.         for (x=0; x < bwidth; x++) {
  77.             min = 1000;
  78.             
  79.             /* find the best character for this block */
  80.             
  81.             for (i=startIndex; i <= endIndex; i++) {
  82.                 if ( (e=CalcError(bp,rowBytes,i,charTab)) < min ) {
  83.                     min = e;
  84.                     minIndex = i;
  85.                 }
  86.                 if ( min == 0 )
  87.                     break;
  88.             }
  89.             *dataPtr++ = minIndex;                    // write char as compressed data
  90.             bp++;
  91.         }
  92.         baseAddr += rowBytes * FONT_HEIGHT;            // bump to next strip
  93.     }
  94.     if ( similarityP )                                // if we did similarity...
  95.         *similarityP = 0;
  96.     return(dataPtr - dataStart);
  97. }
  98.  
  99.  
  100. /************************
  101.  
  102.     Calculate and return the number of pixels that dont match between the given block of 
  103.     1-bit pixels and the equivcalent indexed block in the table
  104.     
  105. ************************/
  106.  
  107. short
  108. CalcError(register char *src,short rowBytes,short index,register char *table)
  109. {
  110.     short err = 0;
  111.     short i,j;
  112.     register signed char d;
  113.     
  114.     table += index;
  115.     
  116.     for ( i=FONT_HEIGHT; i-- ; ) {
  117.         d = *src ^ *table;
  118.         if ( d != 0 ) {
  119.             if ( d == 0xff )
  120.                 err += 8;
  121.             else {
  122.                 for ( j =FONT_WIDTH; j--;  )  {            
  123.                     if ( d < 0 ) 
  124.                         err++;
  125.                     d <<= 1;
  126.                 }
  127.             }
  128.         }
  129.         table += (256 * FONT_WIDTH) / 8;
  130.         src += rowBytes;
  131.     }
  132.     return(err);
  133. }
  134.  
  135.  
  136. /************************
  137.  
  138.     Build a bitmap table of the characters in the font. Keep as shared data.
  139.     
  140. ************************/
  141.  
  142. InitCharTab(SharedGlobals *sGlob,ComponentInstance self)
  143.  
  144. {
  145.     CGrafPtr         savePort;
  146.     GDHandle         saveGD;
  147.     short            x;
  148.     Rect            rect;
  149.     GWorldPtr        gw;
  150.     THz                saveZone;
  151.     Boolean            inAppHeap;
  152.         
  153.     /*
  154.     
  155.         figure out which zone to use, based on where we are loaded.
  156.         
  157.     */
  158.     
  159.     saveZone = GetZone();
  160.     inAppHeap = ( GetComponentInstanceA5(self) != 0 );
  161.     if ( !inAppHeap )
  162.         SetZone(SystemZone());
  163.     sGlob->tableWorld = nil;
  164.     sGlob->table = nil;
  165.     GetGWorld(&savePort,&saveGD);
  166.     SetRect(&rect,0,0,256*FONT_WIDTH,FONT_HEIGHT);
  167.     if ( NewGWorld(&gw,1,&rect,nil,nil,0) == 0 ) {
  168.         (*gw->portPixMap)->rowBytes = 0x8000 | (256 * FONT_WIDTH) / 8;
  169.         SetGWorld(gw,nil);
  170.         TextSize(FONT_SIZE);
  171.         TextFont(FONT_ID);
  172.         EraseRect(&rect);
  173.         ClipRect(&rect);
  174.         for (x=0; x < 256; x++) {
  175.             MoveTo(x*FONT_WIDTH,BASE_LINE);
  176.             DrawChar(x);
  177.         }
  178.         SetGWorld(savePort,saveGD);
  179.         LockPixels(gw->portPixMap);
  180.         sGlob->tableWorld = gw;
  181.         sGlob->table = GetPixBaseAddr(gw->portPixMap);
  182.         
  183.     } 
  184.     SetZone(saveZone);
  185. }
  186.  
  187.  
  188. /************************
  189.  
  190.     Figure duplicate font chars. not used.
  191.     
  192. ************************/
  193.  
  194. MakeSkipTable(SharedGlobals *sGlob)
  195.  
  196. {
  197.     short        i,x;
  198.     char        *charTab = sGlob->table;
  199.     
  200.     for (i=0; i < 256; i++) 
  201.         sGlob->skipTable[i] = 0;
  202.     for (i=0; i < 256; i++) {
  203.         char *icp = charTab + i * FONT_WIDTH;
  204.         if ( !sGlob->skipTable[i] ) {
  205.             for (x=i+1; x < 256; x++) {
  206.                 if ( CalcError(icp,(256 * FONT_WIDTH) / 8,x,charTab) == 0) 
  207.                     sGlob->skipTable[x] = 1;
  208.             }
  209.         }
  210.     }
  211.  
  212. }